package com.isdepci.framework.task;


import com.isdepci.project.carbon.domain.GreenCertificatesListPurchaser;
import com.isdepci.project.carbon.domain.GreenCertificatesSeller;
import com.isdepci.project.carbon.mapper.GreenCertificatesListPurchaserMapper;
import com.isdepci.project.carbon.mapper.GreenCertificatesSellerMapper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;


@Component("greenCertificatesTask")
public class GreenCertificateTask {

    @Autowired
    private GreenCertificatesListPurchaserMapper greenCertificatesListPurchaserMapper;
    @Autowired
    private GreenCertificatesSellerMapper greenCertificatesSellerMapper;

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public void crawlGreenCertificatesData() {
        // 设置Chrome驱动路径
//        System.setProperty("webdriver.chrome.driver", "D:\\jdk1.8.0_181\\bin\\chromedriver.exe");
        System.setProperty("webdriver.chrome.driver", "D:\\develop\\jdk8\\bin\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless");
        WebDriver driver = new ChromeDriver(options);

        try {
            driver.get("http://103.120.198.33:21004/pxf-green-out/home");
            Thread.sleep(5000); // 可以根据需要设置等待时间
            // 获取页面HTML
            String pageSource = driver.getPageSource();
            Document doc = Jsoup.parse(pageSource);
            // 爬取并处理数据
            crawlAndSaveSellerData(doc);
            crawlAndSavePurchaserData(doc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            driver.quit();
        }
    }

    /**
     * 爬取并存储售方挂牌情况数据
     */
    private void crawlAndSaveSellerData(Document doc) throws ParseException {
        greenCertificatesSellerMapper.deleteAllSeller();
        Elements sellerRows = doc.select("div.left-box div.content-box div.el-row");
        for (Element row : sellerRows) {
            Elements cols = row.select("div.el-col");
            if (cols.size() >= 3) {
                GreenCertificatesSeller seller = new GreenCertificatesSeller();
                seller.setSellerName(cols.get(0).text().trim());
                String numStr = cols.get(1).text().trim();
                seller.setGreenCertificatesNum(Long.parseLong(numStr));
                String dateStr = cols.get(2).text().trim();
                Date releaseTime = DATE_FORMAT.parse(dateStr);
                seller.setReleaseTime(releaseTime);
                greenCertificatesSellerMapper.insertGreenCertificatesSeller(seller);
            }
        }
    }

    /**
     * 爬取并存储求购动态数据
     */
    private void crawlAndSavePurchaserData(Document doc) throws ParseException {
        greenCertificatesListPurchaserMapper.deleteALLPurchaser();
        // 选择求购表格的数据行
        Elements purchaserRows = doc.select("div.right-box div.content-box div.el-row");
        for (Element row : purchaserRows) {
            Elements cols = row.select("div.el-col");
            if (cols.size() >= 4) {
                GreenCertificatesListPurchaser purchaser = new GreenCertificatesListPurchaser();
                // 购方名称 (第一列，索引0)
                purchaser.setPurchaserName(cols.get(0).text().trim());
                // 需求数量 (第二列，索引1)
                String demandStr = cols.get(1).text().trim();
                purchaser.setDemandNum(Long.parseLong(demandStr));
                // 单价 (第三列，索引2)
                String priceStr = cols.get(2).text().trim();
                // 将价格字符串转换为 BigDecimal
                BigDecimal unitPrice = new BigDecimal(priceStr.replaceAll(",", "").trim());
                purchaser.setUnitPrice(unitPrice);
                // 发布时间 (第四列，索引3)
                String dateStr = cols.get(3).text().trim();
                Date releaseTime = DATE_FORMAT.parse(dateStr);
                purchaser.setReleaseTime(releaseTime);
                // 保存到数据库
                greenCertificatesListPurchaserMapper.insertGreenCertificatesListPurchaser(purchaser);
            }
        }
    }




}


